package com.li.temperature.chart.bean;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.text.DecimalFormat;

import com.li.temperature.chart.util.Constant;

import android.content.Context;
import android.util.Log;

public abstract class FileNode {

    public Context mContext;

    public String name;

    public String type;

    public String path;

    public String pathType;

    public String sourceData;

    public int valueInt;

    public double valueDoule;

    public String valueStr;

    public String info;

    public boolean isRead;

    public abstract void formatValueInt();

    public abstract void formatValueStr();

    public abstract String getResult();

    public FileNode(Context mContext) {
        this.mContext = mContext;
        this.valueInt = 0;
        this.valueStr = String.valueOf(valueInt);
    }

    public void init() {
        readFile();
        readFileType();
        updateData();
    }

    public void updateData() {
        if (isRead) {
            readFile();
            pareSourceData();
            formatValueInt();
            formatValueStr();
        }
    }

    public void formatTemparaturValue() {
        valueDoule = Math.abs(valueDoule);
        while (valueDoule > 1000) {
            valueDoule = valueDoule / 1000;
        }

        if (valueDoule > 100) {
            valueDoule = valueDoule / 100.0;
        }

        valueDoule = getOneDecimal(valueDoule);

        valueStr = String.valueOf(valueDoule);
    }

    /**
     * 将数据保留两位小数
     */
    private double getOneDecimal(double num) {
        DecimalFormat dFormat = new DecimalFormat("#.0");
        String dString = dFormat.format(num);
        return Double.valueOf(dString);
    }

    public void formatFreqValue() {
        valueInt = valueInt / 1000;
        valueStr = String.valueOf(valueInt);
    }

    private void readFileType() {
        try {
            File mFile = new File(pathType);
            if (mFile != null && mFile.canRead()) {
                String line;
                BufferedReader br = new BufferedReader(new FileReader(pathType));
                if ((line = br.readLine()) != null) {
                    type = line;
                    //Log.d(Constant.TAG, "FilePath : " + path + ", isRead : " + isRead + "\n type :" + type);
                }
                br.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void readFile() {
        try {
            File mFile = new File(path);
            if (mFile != null && mFile.canRead()) {
                String line;
                BufferedReader br = new BufferedReader(new FileReader(path));
                if ((line = br.readLine()) != null) {
                    sourceData = line;
                }
                br.close();
                isRead = true;
            } else {
                isRead = false;
                sourceData = "Can't read";
            }
        } catch (Exception e) {
            e.printStackTrace();
            sourceData = e.toString();
            isRead = false;
        }

        //Log.d(Constant.TAG, "FilePath : " + path + ", isRead : " + isRead + "\n SourceData :" + sourceData);
    }

    private void pareSourceData() {
        valueInt = Integer.valueOf(sourceData);
        valueDoule = Double.valueOf(sourceData);
        //Log.d(Constant.TAG, "pareSourceData valueInt: " + valueInt + ", valueDoule = " + valueDoule);
    }
}
